package com.wu.backend.service.impl;


import com.wu.backend.entity.*;
import com.wu.backend.mapper.*;
import com.wu.backend.service.BigDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class BigDataServiceImpl implements BigDataService {

    @Autowired
    NewEnergyChargeWeekPersonMapper newEnergyChargeWeekPersonMapper;
    @Autowired
    NewEnergyChargeWeekChargeTimeMapper newEnergyChargeWeekChargeTimeMapper;

    @Autowired
    DateStatMapper dateStatMapper;
    @Autowired
    TimeStatMapper timeStatMapper;

    @Autowired
    NewEnergyChargeWeekMapper newEnergyChargeWeekMapper;

    @Autowired
    UserBehaviorMapper userBehaviorMapper;
    @Override
    public List<UserBehavior> queryUserBehavior() {
        return userBehaviorMapper.queryUserBehavior();
    }

    @Override
    public List<NewEnergyChargeWeekPerson> queryNewEnergyChargeWeekPerson() {
        return newEnergyChargeWeekPersonMapper.queryNewEnergyChargeWeekPerson();
    }

    @Override
    public List<NewEnergyChargeWeekChargeTime> queryNewEnergyChargeWeekChargeTime() {
        return newEnergyChargeWeekChargeTimeMapper.queryNewEnergyChargeWeekChargeTime();
    }

    @Override
    public DateStatVo queryDateStat() {
        List<DateStat> entityList = dateStatMapper.queryDateStat();
        List<String> typeList = Arrays.asList("pv","fav","cart","buy");
        // 组装日期列表，第一个列表用于返回前端作为X轴，第二个列表用于后续循环处理数据
        List<String> dateStrList = new ArrayList<>();
        Set<Date> uniqueDates = new LinkedHashSet<>();

        // 填充两个列表
        for (DateStat item: entityList) {
            if (!uniqueDates.contains(item.getDate())){
                uniqueDates.add(item.getDate());
                SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd");
                String dateStr = sdf.format(item.getDate());
                dateStrList.add(dateStr);
            }
        }

        // 返回给前端的数据对象
        DateStatVo dateStatVo = new DateStatVo();
        dateStatVo.setDateList(dateStrList);
        dateStatVo.setEventCnt(new ArrayList<>());

        // 按顺序组装每个行为对应的每日行为信息
        for (String type: typeList) {
            List<Long> cntList = new ArrayList<>();
            for (Date itemDate: uniqueDates) {
                long cnt = entityList.stream().filter(v -> v.getDate().equals(itemDate)
                                && v.getEventType().equals(type))
                        .map(DateStat::getCnt).findFirst().orElse(Long.valueOf("0"));
                cntList.add(cnt);
            }
            dateStatVo.getEventCnt().add(cntList);
        }

        return dateStatVo;
    }
    @Override
    public TimeStatVo queryTimeStat() {
        List<TimeStat> entityList = timeStatMapper.queryTimeStat();
        List<String> typeList = Arrays.asList("pv","fav","cart","buy");
        // 组装时间列表，第一个列表用于返回前端作为X轴，第二个列表用于后续循环处理数据
        List<String> timeStrList = new ArrayList<>();
        Set<String> uniqueTimes = new LinkedHashSet<>();
        //Set<String> uniqueTimes = new HashSet<>();

        // 填充两个列表
        for (TimeStat item: entityList) {
            if (!uniqueTimes.contains(item.getTime())){
                uniqueTimes.add(item.getTime());
                timeStrList.add(item.getTime());
            }
        }
//        List<String> sortedTimes = uniqueTimes.stream()
//                .sorted(Comparator.comparingInt(Integer::parseInt)) // 将字符串转换为数字后排序
//                .collect(Collectors.toList());

        // 返回给前端的数据对象
        TimeStatVo timeStatVo = new TimeStatVo();
        timeStatVo.setTimeList(timeStrList);
        timeStatVo.setEventCnt(new ArrayList<>());

        // 按顺序组装每个行为对应的每小时行为信息
        for (String type: typeList) {
            List<Long> cntList = new ArrayList<>();
            for (String itemTime: uniqueTimes) {
                long cnt = entityList.stream().filter(v -> v.getTime().equals(itemTime) && v.getEventType().equals(type))
                        .map(TimeStat::getCnt).findFirst().orElse(Long.valueOf("0"));
                cntList.add(cnt);
            }
            timeStatVo.getEventCnt().add(cntList);
        }

        return timeStatVo;
    }

    @Override
    public NewEnergyChargeWeekVo queryNewEnergyChargeWeek() {
        List<NewEnergyChargeWeek> entityList = newEnergyChargeWeekMapper.queryNewEnergyChargeWeek();

        // 组装时间列表，第一个列表用于返回前端作为X轴，第二个列表用于后续循环处理数据
        List<String> weekStrList = new ArrayList<>();
        Set<String> uniqueWeeks = new LinkedHashSet<>();
        //Set<String> uniqueTimes = new HashSet<>();

        // 填充两个列表
        for (NewEnergyChargeWeek item: entityList) {
            if (!uniqueWeeks.contains(item.getWeek())){
                uniqueWeeks.add(item.getWeek());
                weekStrList.add(item.getWeek());
            }
        }

        // 返回给前端的数据对象
        NewEnergyChargeWeekVo newEnergyChargeWeekVo = new NewEnergyChargeWeekVo();
        newEnergyChargeWeekVo.setWeekList(weekStrList);
        newEnergyChargeWeekVo.setDataCnt(new ArrayList<>());


        List<String> cntList = new ArrayList<>();
        for (String itemWeek: uniqueWeeks) {
            String cnt = entityList.stream().filter(v -> v.getWeek().equals(itemWeek))
                    .map(NewEnergyChargeWeek::getPersonCnt).findFirst().orElse(String.valueOf(Long.valueOf("0")));
            cntList.add(cnt);
        }
        newEnergyChargeWeekVo.getDataCnt().add(cntList);
        cntList = new ArrayList<>();
        for (String itemWeek: uniqueWeeks) {
            String cnt = entityList.stream().filter(v -> v.getWeek().equals(itemWeek))
                    .map(NewEnergyChargeWeek::getChargeTime).findFirst().orElse(String.valueOf(Long.valueOf("0")));
            cntList.add(cnt);
        }
        newEnergyChargeWeekVo.getDataCnt().add(cntList);
        cntList = new ArrayList<>();
        for (String itemWeek: uniqueWeeks) {
            String cnt = entityList.stream().filter(v -> v.getWeek().equals(itemWeek))
                    .map(NewEnergyChargeWeek::getChargeFee).findFirst().orElse(String.valueOf(Long.valueOf("0")));
            cntList.add(cnt);
        }
        newEnergyChargeWeekVo.getDataCnt().add(cntList);


        return newEnergyChargeWeekVo;
    }

}